#!/bin/sh
/usr/sbin/xinetd -d &

HOME=/home/kx
export HOME

target=$NBROOT
if find "$target" -mindepth 1 -print -quit 2>/dev/null | grep -q .; then
    echo "Not empty, do nothing"
else
    echo "'$NBROOT' is empty, copying default file" && cp -R /opt/kx/jupyterq/examples $NBROOT && cp /opt/kx/jupyterq/kdb+Notebooks.ipynb $NBROOT
fi

chmod -R a+w $NBROOT
chmod -R a+w $NBDATA

printf '\npoint your browser at http://localhost:%s\n\n' "$PORT"

exec chpst -u kx /bin/sh -l -c "cd /opt/kx/jupyterq && QLIC=/home/kx exec jupyter notebook --notebook-dir='$NBROOT' --ip='0.0.0.0' --port='$PORT' --no-browser"
